<?php
if (! function_exists('timeTransfer')) {
    function timeTransfer(int $time, string $format = 'Y-m-d H:i:s') {
        $duration = time() - $time;

        if ($duration < 0) {
            return $time;
        }

        if ($duration < 60) {
            return $duration . '秒前';
        }

        if ($duration < 60 * 60) {
            return floor($duration / 60).'分钟前';
        }

        if ($duration < 24 * 60 * 60) {
            return floor($duration / 3600).'小时前';
        }

        if ($duration < 30 * 24 * 60 * 60) {
            return floor($duration / 86400).'天前';
        }

        return date($format, $time);
    }
}
